rm(list=ls())
pacman::p_load(tidyverse, magrittr, ggdist, cowplot, ggh4x)
source("code/theme_cust.R")

## This script takes the environment saved in Getting_PanelEstimates.R
## and Getting_PanelEstimates.R

load("data/tmp/PanelMatch_temp2.RData")

list_results <- list(results_greens_west_entry_ci95, results_greens_west_entry_ci90)

results_df <- map_dfr(list_results, function (x) {
    results <- map_dfr(x, as_tibble, .id = "model") %>%
        separate(
            col = model,
            sep = " ",
            into = c(
                "lag",
                "lead",
                "matching",
                "method",
                "nbr_matches",
                "reversal",
                "qoi"
            ),
        ) %>%
        filter(matching != "no")
},
.id = "sample")

# further cleaning

results_df %<>%
    mutate(
        treat = case_when(
            sample %in% c(1,2) ~ "Estimated Effect of\nGreens Entry",
            sample %in% c(3,4) ~ "Estimated Effect of\nGreens Exit",

        ),
        reg = "West Germany",
        nbr_matches = case_when(nbr_matches == 10 ~ "Up to 10 matches",
                                nbr_matches == 5 ~ "Up to 5 matches"
        ),
        method = case_when(method == "mahalanobis" ~ "Mahalanobis Matching",
                           method == "CBPS.match" ~ "Propensity Score Matching",
                           method == "CBPS.weight" ~ "Propensity Score\nWeighting"),
        across(c(treat, reg, nbr_matches, method), as.factor),
        method = fct_relevel(method, "Propensity Score\nWeighting", after = Inf),
        
    ) %>% 
    rename(.width = CI) %>% 
    filter(!lead %in% c(2,3))

results_west <- results_df %>% filter(reg == "West Germany") 

p_west_entry <- results_west %>% 
    filter(treat == "Estimated Effect of\nGreens Entry") %>% 
    ggplot(., 
       aes(x = lead, y = P,
           ymin = L,
           ymax = U,
           colour = lag)) +
    geom_pointinterval(
        position = position_dodge(width = .3),
        interval_size_range = c(1, 1.5),
        fatten_point = .9,
        point_fill = "white") +
    geom_hline(aes(yintercept = 0), colour = "black", linetype = "dashed", alpha = .85) +
    scale_y_continuous(position = "right") +
    scale_colour_grey() +
    xlab("Elections Relative to the administration of treatment") +
    ylab("") +
    guides(colour = guide_legend(title = "Number of Lags")) +
    facet_nested(reg + treat ~ method + nbr_matches,
                 scales = "free_y", 
                 switch = "y",) + 
    theme_hanno() +
    theme(
        legend.position = "bottom")

p_west_entry
# p_west_exit <- results_west %>% 
#     filter(treat == "Estimated Effect of\nGreens Exit") %>% 
#     ggplot(., 
#            aes(x = lead, y = P,
#                ymin = L,
#                ymax = U,
#                colour = lag)) +
#     geom_pointinterval(
#         position = position_dodge(width = .3),
#         interval_size_range = c(1, 1.5),
#         fatten_point = .9,
#         point_fill = "white") +
#     geom_hline(aes(yintercept = 0), colour = "black", linetype = "dashed", alpha = .85) +
#     scale_y_continuous(position = "right") +
#     scale_colour_grey() +
#     xlab("Elections Relative to the administration of treatment") +
#     ylab("") +
#     guides(colour = guide_legend(title = "Number of Lags")) +
#     facet_nested(reg + treat ~ method + nbr_matches,
#                  scales = "free_y", 
#                  switch = "y",) + 
#     theme_cust() +
#     theme(
#         legend.position = "bottom")
# p_west_exit
ggsave(plot = p_west_entry, filename = "output/figures/fig3.png", device = "png", height = 6, width = 10, dpi = 600)
# ggsave(plot = p_west_exit, filename = "output/figures/results_west_ger_exit.png", device = "png", height = 6, width = 10, dpi = 600)

